﻿using Lingmeng.EntityFrameworkCore.core;
using Lingmeng.EntityFrameworkCore.Extensions;
using Microsoft.EntityFrameworkCore;
using Video.Domain.Shared;
using Video.Domain.VideoRepository;
using Video.Domain.Videos;

namespace Video.EntityFrameworkCore.Videos
{
    public class VideoRepository : Repository<VideoDbContext, Domain.Videos.Video>, IVideoRepository
    {
        public VideoRepository(VideoDbContext dbContext) : base(dbContext)
        {

        }

        public async Task CreateClassifyAsync(string name)
        {
            await _dbContext.Classify.AddAsync(new Classify()
            {
                Name = name
                
            });
            await _dbContext.SaveChangesAsync();
        }

        public async Task DeleteClassifyAsyn(Guid id)
        {
            try
            {
                var query = await _dbContext.Classify.FirstOrDefaultAsync(x => x.Id == id);
                if (query != null)
                {
                    _dbContext.Classify.Remove(query);
                    await _dbContext.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                // 返回友好的错误提示信息
                throw new BusinessException("删除失败");
            }
        }

        public Task<List<Classify>> GetClassifyListAsync(string? name)
        {
            return _dbContext.Classify.WhereIf(!string.IsNullOrEmpty(name),x=>EF.Functions.Like(x.Name, $"%{name}%")).ToListAsync();
        }
        public async Task UpdateClassifyAsync(Classify classify)
        {
            _dbContext.Classify.Update(classify);
            await _dbContext.SaveChangesAsync();
            await Task.CompletedTask;
        }

        public async Task<List<Domain.Videos.Video>> GetListVideoAsync(Guid? userId, Guid? classify, string? keywords, DateTime? startTime, DateTime? endTime, int skipCount, int maxResultCount)
        {
            var query = CreateQuery(userId, classify, keywords, startTime, endTime);
            return await query.PageBy(skipCount, maxResultCount).ToListAsync();
        }

        public async Task<int> GetVideoCountAsync(Guid? userId, Guid? classify, string? keywords, DateTime? startTime, DateTime? endTime)
        {
            var query=CreateQuery(userId, classify, keywords, startTime, endTime);
            return await query.CountAsync();
        }



        private IQueryable<Domain.Videos.Video> CreateQuery(Guid? userId, Guid? classify, string? keywords, DateTime? startTime, DateTime? endTime)
        {
            var query =
                _dbContext.Video.
                WhereIf(userId.HasValue, x => x.UserId == userId)
                .WhereIf(classify.HasValue, x => x.ClassifyId == classify)
                .WhereIf(!string.IsNullOrEmpty(keywords), x => EF.Functions.Like(x.Title, $"%{keywords}%"))
                .WhereIf(startTime.HasValue, x => x.CreatedTime >= startTime)
                .WhereIf(endTime.HasValue, x => x.CreatedTime <= endTime);
            return query;

        }


    }
}
